package org.fuxian.controller;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.fuxian.dao.FeedBackMapper;
import org.fuxian.dao.UserInfoMapper;
import org.fuxian.model.FeedBack;
import org.fuxian.model.UserInfo;
import org.fuxian.util.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    FeedBackMapper feedBackMapper;


    @RequestMapping("/test")
    @ResponseBody
    public Map<String, Object> login() {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("flag", "error");
        return returnMap;
    }

    @RequestMapping("/login")
    @ResponseBody
    public Map<String, Object> login(String userName, String pwd, HttpSession session) {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("flag", "ok");
        session.setAttribute("USER", "fuxian");
        return returnMap;
    }

    @RequestMapping("/user/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("code", "200");

        PageHelper.startPage(page, 5);
        List<UserInfo> userInfoList = userInfoMapper.selectAll();
        PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfoList, 5);
        returnMap.put("pageInfo", pageInfo);
        return returnMap;
    }


    @RequestMapping("/user/getOneById")
    @ResponseBody
    public Map<String, Object> getOne(@RequestParam(value = "id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(id);

        if (userInfo != null) {
            map.put("code", "200");
            map.put("userInfo", userInfo);
        } else {
            map.put("code", "500");
        }
        return map;
    }

    @RequestMapping("/user/update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody(required = false) UserInfo userInfo) {
        Map<String, Object> map = new HashMap<>();
        int count = userInfoMapper.updateByPrimaryKey(userInfo);
        if (count > 0) {
            map.put("code", "200");
        } else {
            map.put("code", "500");
        }
        return map;
    }

    @RequestMapping("/user/new")
    @ResponseBody
    public Map<String, Object> insert(@RequestBody(required = false) UserInfo userInfo) {
        Map<String, Object> map = new HashMap<>();

        int count = userInfoMapper.insert(userInfo);
        if (count > 0) {
            map.put("code", "200");
        } else {
            map.put("code", "500");
        }
        return map;
    }


    @RequestMapping("/user/del")
    @ResponseBody
    public Map<String, Object> delete(@RequestParam(value = "id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        int count = userInfoMapper.deleteByPrimaryKey(id);
        if (count > 0) {
            map.put("code", "200");
        } else {
            map.put("code", "500");
        }
        return map;
    }


    @RequestMapping("/newFile")
    @ResponseBody
    public Map<String, Object> userFile(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String newPath = "img" + File.separator;
        newPath += UploadUtils.upload(request, file);
        map.put("code", "200");
        map.put("newPath", newPath);
        return map;
    }


    @RequestMapping("/feedBack/list")
    @ResponseBody
    public Map<String, Object> feedBacklist(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "id") Integer id) {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("code", "200");

        PageHelper.startPage(page, 5);
        List<FeedBack> feedBackList = feedBackMapper.selectAllByUserId(id);
        PageInfo<FeedBack> pageInfo = new PageInfo<>(feedBackList, 5);
        returnMap.put("pageInfo", pageInfo);
        return returnMap;
    }


    @RequestMapping("/user/newFb")
    @ResponseBody
    public Map<String, Object> newFb(@RequestBody(required = false) FeedBack feedBack) {
        Map<String, Object> map = new HashMap<>();
        feedBack.setDate(DateUtil.today());
        int count = feedBackMapper.insert(feedBack);
        if (count > 0) {
            map.put("code", "200");
        } else {
            map.put("code", "500");
        }
        return map;
    }

}
